package com.dao;

import com.beans.AdminInfo;
import com.beans.CateInfo;
import com.beans.GoodsInfo;
import com.beans.PageInfo;
import com.jdbc.DBUtil;

import java.util.List;

public class GoodsDao {
    //添加商品,并返回添加商品的id
    public int addGoods(GoodsInfo goods){
        String sql= "insert into goodsInfo (goodsName,price,unit,des,bigCateId,smallCateId,producter,pictureData) values(?,?,?,?,?,?,?,?)";
        Object [] params = {
                goods.getGoodsName(),
                goods.getPrice(),
                goods.getUnit(),
                goods.getDes(),
                goods.getBigCateId(),
                goods.getSmallCateId(),
                goods.getProducter(),
                goods.getPictureData()
        };
        return DBUtil.addWithId(sql,params);
    }

    public GoodsInfo getGoodsById(int goodsId) {
        return DBUtil.getSingleObj("select * from goodsInfo where id=?",GoodsInfo.class,goodsId);
    }

    //获取商品总数(分页)
    public int getGoodsCount(String bigCateId,String smallCateId,String goodsName) {
        String sql= "select count(*) from goodsInfo where 1=1 " ;

        if(bigCateId!=""&&bigCateId!=null){
            sql+=" and bigCateId="+bigCateId;
        }

        if(smallCateId!=""&&smallCateId!=null){
            sql+=" and smallCateId="+smallCateId;
        }

        if(goodsName!=""&&goodsName!=null){
            sql+=" and goodsName like '%"+goodsName+"%'";
        }

        return new Integer(DBUtil.getScalar(sql)+"");
    }

    //分页获取商品列表(多条件查询)
    public List<GoodsInfo> getAllGoods(String bigCateId, String smallCateId, String goodsName,PageInfo page) {
        String sql= " select temp.*,b.cateName as smallCateName from "
                + "(select a.*,b.cateName as bigCateName from goodsInfo a left join cateInfo b on a.bigcateId=b.id) temp "
                + " left join cateInfo b on temp.smallcateId=b.id  where 1=1 " ;

        if(bigCateId!=""&&bigCateId!=null){
            sql+=" and bigCateId="+bigCateId;
        }

        if(smallCateId!=""&&smallCateId!=null){
            sql+=" and smallCateId="+smallCateId;
        }

        if(goodsName!=""&&goodsName!=null){
            sql+=" and goodsName like '%"+goodsName+"%'";
        }

        sql+=" order by editDate desc limit ?,?";
        return DBUtil.getList(sql, GoodsInfo.class,page.getBeginRow(),page.getPageSize());
    }

    public GoodsInfo getGoodsByCateId(int id, CateInfo cateInfo) {
        String sql="";
        if (cateInfo.getParentId()==0){
            sql = "select * from goodsInfo where bigCateId=?";
        }else {
            sql = "select * from goodsInfo where smallCateId=?";
        }
        return DBUtil.getSingleObj(sql,GoodsInfo.class,id);
    }

    public int deleteGoods(int id) {
        String sql = "delete from goodsInfo where id=?";
        return DBUtil.update(sql,id);
    }

    public int updateGoods(GoodsInfo goods) {
        String sql ="";
        if (goods.getPictureData().length!=0) {
            sql = "update goodsInfo set goodsName=?,price=?,unit=?,des=?,bigCateId=?,smallCateId=?,producter=?,pictureData=? where id=?";
            Object[] params = {
                    goods.getGoodsName(),
                    goods.getPrice(),
                    goods.getUnit(),
                    goods.getDes(),
                    goods.getBigCateId(),
                    goods.getSmallCateId(),
                    goods.getProducter(),
                    goods.getPictureData(),
                    goods.getId()
            };
            return DBUtil.update(sql, params);
        }else {
            sql = "update goodsInfo set goodsName=?,price=?,unit=?,des=?,bigCateId=?,smallCateId=?,producter=? where id=?";
            Object[] params = {
                    goods.getGoodsName(),
                    goods.getPrice(),
                    goods.getUnit(),
                    goods.getDes(),
                    goods.getBigCateId(),
                    goods.getSmallCateId(),
                    goods.getProducter(),
                    goods.getId()
            };
            return DBUtil.update(sql, params);
        }
    }
}
